'''###########################################################################################################################
### File: rayman-panda3d.py
### Name: Patrick Delaney, Andrew Oberlin
### Class: CSE 487
### Instructor: Dr.Zmuda
### Assignment: Final Project
### Description: Class for handling the gui for the game maker
################ 
'''
from menu_button import MenuButton
from panda3d.core import Vec2, Point3, Vec3, Vec4
from direct.gui.DirectGui import *
import os
from character_gen import CharacterGen
from object_gen import ObjectGen
from level_gen import LevelGen

class GameMakerGUI:
	'''
	### Name: __init__ (Constructor)
	### Author: Andrew Oberlin, Patrick Delaney
	### Parameters: None
	### Description: Initializes all the game maker gui, which
					 requires a project name to move on
	'''
	def __init__(self, parent,mainGui):
		self.parent = parent
		self.models = dict()
		self.mainGui = mainGui
		self.projectName = ""
		self.createCharacter = None
		self.createObject = None
		self.createLevel = None
		self.__loadModels()
		
	'''
	### Name: loadModels (Constructor)
	### Author: Andrew Oberlin, Patrick Delaney
	### Parameters: None
	### Description: Loads the GUI objects
	'''
	def __loadModels(self):
		self.models['background'] = self.parent.loader.loadModelCopy("models/plane")
		self.backgroundTexture = self.parent.loader.loadTexture("models/gui/game_maker/menu_background.jpg")
		self.models['background'].setTexture(self.backgroundTexture)
		self.models['background'].setTransparency(1)
		self.models['background'].setScale(2.67, 1, 2)
		self.models['background'].reparentTo(self.parent.base.aspect2d)
		
		self.models['logo'] = self.parent.loader.loadModelCopy("models/plane")
		self.logoTexture = self.parent.loader.loadTexture("models/gui/game_maker/game_maker_logo.png")
		self.models['logo'].setTexture(self.logoTexture)
		self.models['logo'].setTransparency(1)
		self.models['logo'].setScale(2.67, 1, 0.35)
		self.models['logo'].setPos(0, 0, 0.75)
		self.models['logo'].reparentTo(self.parent.base.aspect2d)
		
		#def MenuButton(self, parent, name, texturePath, command, pos, numFrames = 10, scale=0.5,fps=5,
		#	textureExt='png', isMovie = True, isInterval = False, text=None, textPos=None,
		#	textScale=None, extraArgs=[]):
		
		self.models['create_character_button'] = MenuButton(self.parent, 'create_character_button', 'models/gui/game_maker/create_character_button', 
			self.loadCreateCharacter, Vec3(-.75, 0, 0.25), textPos=Vec3(-0.35, 0, .30), isMovie = False, text="Create Character")
		self.models['create_character_button'].hide()
		
		self.models['create_object_button'] = MenuButton(self.parent, 'create_object_button', 'models/gui/game_maker/create_object_button', 
			self.loadCreateObject, Vec3(.75, 0, 0.25), textPos=Vec3(-0.35, 0, .30), isMovie = False, text="Create Object")
		self.models['create_object_button'].hide()
		
		self.models['create_level_button'] = MenuButton(self.parent, 'create_level_button', 'models/gui/game_maker/create_level_button', 
			self.loadCreateLevel, Vec3(0, 0, -0.35), textPos=Vec3(-0.275, 0, .30), isMovie = False, text="Create Level")
		self.models['create_level_button'].hide()
		
		self.models['load_button'] = MenuButton(
			self.parent, 'load', 'models/gui/game_maker/dialog_button', 
			self.showLoadProject, Vec3(-0.45, 0, 0), isMovie = False, text="Load Project", 
			textPos=Vec3(-0.30, 0, -0.025), scale=Vec3(0.7,1,0.15)
		)
			
		self.models['new_button'] = MenuButton(
			self.parent, 'new', 'models/gui/game_maker/dialog_button', 
			self.showNewProject, Vec3(0.45, 0, 0), isMovie = False, text="New Project", 
			textPos=Vec3(-0.30, 0, -0.025), scale=Vec3(0.7,1,0.15)
		)
		
		# loads a project with the name in the input
		self.models['load_input_dummy'] = self.parent.base.aspect2d.attachNewNode('load_input_dummy')
		
		self.models['load_input'] = DirectEntry(initialText="Project Name", cursorKeys=True, scale=Vec3(0.10, 0.075, 0.075))
		self.models['load_input'].setPos(Vec3(0, 0, -0.025))
		self.models['load_input'].reparentTo(self.models['load_input_dummy'])
		
		self.models['load_confirm_button'] = MenuButton(
			self.parent, 'load_confirm', 'models/gui/game_maker/check', 
			self.loadProject, Vec3(1.1, 0, 0), isMovie = False, scale=Vec3(0.10, 0.10, 0.10)
		)
		
		self.models['load_cancel_button'] = MenuButton(
			self.parent, 'load_cancel', 'models/gui/game_maker/cancel', 
			self.cancelLoad, Vec3(1.25, 0, 0), isMovie = False, scale=Vec3(0.10, 0.10, 0.10)
		)
		
		self.models['load_confirm_button'].reparentTo(self.models['load_input_dummy'])
		self.models['load_cancel_button'].reparentTo(self.models['load_input_dummy'])
		
		# starts a new project with the name in the input
		self.models['new_input_dummy'] = self.parent.base.aspect2d.attachNewNode('new_input_dummy')
		
		self.models['new_input'] = DirectEntry(initialText="Project Name", cursorKeys=True, scale=Vec3(0.10, 0.075, 0.075))
		self.models['new_input'].setPos(Vec3(0, 0, -0.025))
		self.models['new_input'].reparentTo(self.models['new_input_dummy'])
		
		self.models['new_confirm_button'] = MenuButton(
			self.parent, 'new_confirm', 'models/gui/game_maker/check', 
			self.newProject, Vec3(1.1, 0, 0), isMovie = False, scale=Vec3(0.10, 0.10, 0.10)
		)
		
		self.models['new_cancel_button'] = MenuButton(
			self.parent, 'new_cancel', 'models/gui/game_maker/cancel', 
			self.cancelNew, Vec3(1.25, 0, 0), isMovie = False, scale=Vec3(0.10, 0.10, 0.10)
		)
		
		self.models['new_confirm_button'].reparentTo(self.models['new_input_dummy'])
		self.models['new_cancel_button'].reparentTo(self.models['new_input_dummy'])
		
		self.models['new_input_dummy'].hide()
		self.models['load_input_dummy'].hide()
		
		self.models['back_button'] = MenuButton(self.parent, 'create_object_button', 'models/gui/game_maker/back_button', 
			self.goBack, Vec3(-.90, 0, -.80),scale=0.25, textPos=Vec3(-0.20, 0, .15), isMovie = False, text="Go Back")
			
	'''
	### Name: __init__ (Constructor)
	### Author: Andrew Oberlin, Patrick Delaney
	### Parameters: None
	### Description: Creates the input box for loading the project
	'''		
	def showLoadProject(self):
		self.models['new_button'].hide()
		self.models['load_input_dummy'].show()
		
	'''
	### Name: showNewProject (Constructor)
	### Author: Andrew Oberlin, Patrick Delaney
	### Parameters: None
	### Description: Creates the input box for a new project
	'''		
	def showNewProject(self):
		self.models['load_button'].hide()
		self.models['new_button'].setPos(Vec3(-0.45, 0, 0))
		self.models['new_input_dummy'].show()
			
	
	'''
	### Name: loadProject (Constructor)
	### Author: Andrew Oberlin, Patrick Delaney
	### Parameters: None
	### Description: Loads a project by checking for directory existence
	'''		
	def loadProject(self, projectName=None):
		if (not projectName):
			projectName = self.models['load_input'].get(plain=True)
		projectDir = 'projects/' + projectName
		if (os.path.isdir(projectDir)):
			if (os.path.isdir(projectDir + "/characters")):
				if (os.path.isdir(projectDir + "/characters/main")):
					if (os.path.isdir(projectDir + "/characters/enemies")):
						if (os.path.isdir(projectDir + "/objects")):
							if (os.path.isdir(projectDir + "/levels")):
								self.projectName = projectName
								self.startGameMaker(projectName)
		
	'''
	### Name: __init__ (Constructor)
	### Author: Andrew Oberlin, Patrick Delaney
	### Parameters: None
	### Description: Cancels the loading of an existing project
	'''		
	def cancelLoad(self):
		self.models['new_button'].show()
		self.models['load_input_dummy'].hide()
		
	'''
	### Name: __init__ (Constructor)
	### Author: Andrew Oberlin, Patrick Delaney
	### Parameters: None
	### Description: Loads a project by checking for directory existence
	'''		
	def newProject(self):
		projectName = self.models['new_input'].get(plain=True)
		projectDir = 'projects/' + projectName
		if (not os.path.isdir(projectDir)):
			os.mkdir(projectDir)
			os.mkdir(projectDir + "/characters")
			os.mkdir(projectDir + "/characters/main")
			os.mkdir(projectDir + "/characters/enemies")
			os.mkdir(projectDir + "/objects")
			os.mkdir(projectDir + "/levels")
			self.projectName = projectName
			self.startGameMaker(projectName)
		else:
			self.projectName = projectName
			self.loadProject(projectName)
			
	'''
	### Name: __init__ (Constructor)
	### Author: Andrew Oberlin, Patrick Delaney
	### Parameters: None
	### Description: Cancels the loading of an existing project
	'''		
	def cancelNew(self):
		self.models['load_button'].show()
		self.models['new_button'].setPos(Vec3(0.45, 0, 0))
		self.models['new_input_dummy'].hide()
		
	'''
	### Name: startGameMaker (Constructor)
	### Author: Andrew Oberlin, Patrick Delaney
	### Parameters: None
	### Description: Cancels the loading of an existing project
	'''	
	def startGameMaker(self, projectName):
		self.models['load_input_dummy'].hide()
		self.models['new_input_dummy'].hide()
		self.models['new_button'].hide()
		self.models['load_button'].hide()
		
		self.models['create_character_button'].show()
		self.models['create_object_button'].show()
		self.models['create_level_button'].show()
		
	'''
	### Name: startGameMaker (Constructor)
	### Author: Andrew Oberlin, Patrick Delaney
	### Parameters: None
	### Description: Cancels the loading of an existing project
	'''	
	def loadCreateCharacter(self):
		if (self.projectName):
			#if (self.createCharacter):
			#	self.createCharacter.destroy()
			self.hide()
			self.createCharacter = CharacterGen(self.parent, self.projectName,self)
	
	'''
	### Name: startGameMaker (Constructor)
	### Author: Andrew Oberlin, Patrick Delaney
	### Parameters: None
	### Description: Cancels the loading of an existing project
	'''	
	def loadCreateObject(self):
		if (self.projectName):
			#if (self.createObject):
				#self.createObject.destroy()
			self.hide()
			self.createObject = ObjectGen(self.parent, self.projectName,self)
	
	'''
	### Name: startGameMaker (Constructor)
	### Author: Andrew Oberlin, Patrick Delaney
	### Parameters: None
	### Description: Cancels the loading of an existing project
	'''	
	def loadCreateLevel(self):
		if (self.projectName):
			#if (self.createLevel):
			#	self.createLevel.destroy()
			self.hide()
			self.createLevel = LevelGen(self.parent, self.projectName,self)
	
		'''
	### Name: hide
	### Author: Andrew Oberlin, Patrick Delaney
	### Parameters: None
	### Description: hides this GUI
	'''
	def hide(self):
		for key in self.models.keys():
			self.models[key].hide()
	'''
	### Name: goBack
	### Author: Andrew Oberlin, Patrick Delaney
	### Parameters: None
	### Description: goes back to the previous GUI
	'''
	def goBack(self):
		self.hide()
		self.mainGui.show()
	